<?php
include './include/common.php';
if($_GET['pinyin']){
$pinyin=$_GET['pinyin'];
}elseif($_POST['button2']){
$pinyin='';
}elseif($_POST['city']){
$pinyin=$_POST['city'];
}

if (!$pinyin&&empty($_COOKIE['wcity']['id'])) { //第一次访问，没有地区和cookie
	include SYSTEM_ROOTPATH . '/include/ipcha.php'; //模版编译类
	$myobj = new ipLocation();
	$IP = $myobj->get_IP();
	$address = $myobj->getaddress($IP );
	$myobj = NULL;
	$address["area1"]=mb_convert_encoding($address["area1"],"UTF-8","GBK" );
	$wordCity = $db->select ( "select `id`,`name`,`pinyin`,`Ab` from " . $table_top . "area where property=1" );
	foreach ( $wordCity as $v ) {
		if (strstr( $address ['area1'],$v['Ab'])) {
			SetCookie ( "wcity[pinyin]", $v ['pinyin'], time () + 600, "/", $SCRIPT_HOST );
			SetCookie ( "wcity[id]", $v ['id'], time () + 600, "/", $SCRIPT_HOST );
			SetCookie ( "wcity[name]", $v ['Ab'], time () + 600, "/", $SCRIPT_HOST );
			$_COOKIE ['wcity']['pinyin']=$v ['pinyin'];
			$_COOKIE ['wcity']['id']=$v ['id'];
			$_COOKIE ['wcity']['name']=$v ['Ab'];
			break;
		}
	}
	if (!$_COOKIE ['wcity']['name']) {
			SetCookie ( "wcity[pinyin]", $v ['pinyin'], time () + 600, "/", $SCRIPT_HOST );
			SetCookie ( "wcity[id]", $v ['id'], time () + 600, "/", $SCRIPT_HOST );
			SetCookie ( "wcity[name]", $v ['name'], time () + 600, "/", $SCRIPT_HOST );
			$_COOKIE ['wcity']['pinyin']='shanghai';
			$_COOKIE ['wcity']['id']='310000';
			$_COOKIE ['wcity']['name']='上海';
			
		}
}

$pinyin2=$pinyin?$pinyin:$_COOKIE ['wcity']['pinyin'];	
		

$rs=$db->getone("select id,name,Ab from {$table_top}area where pinyin='{$pinyin2}'");
$id=$rs['id'];
$name=$rs['name'];
$add=$pinyin?'&&id='.$id:'';
$link=$add?"<a href='http://www.tianqi.la/{$pinyin2}.html\"><b>{$rs['Ab']}</b></a>":"<a href='http://www.tianqi.la/\">天气啦</a>";


include template('dy'); 
?>
